package net.yla.smartbus.module.controller.web.ba.area;

import net.yla.smartbus.core.api.ApiResult;
import net.yla.smartbus.core.base.BaseController;
import net.yla.smartbus.core.base.PageBean;
import net.yla.smartbus.module.ba.area.dto.AreaDto;
import net.yla.smartbus.module.ba.area.form.AreaForm;
import net.yla.smartbus.module.ba.area.query.AreaQuery;
import net.yla.smartbus.module.ba.area.serice.AreaService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 默苍离
 * @date 2021/6/26 10:19
 * <p>
 * ******************************************
 * @annotation:
 */
@RestController
@RequestMapping("/api/web/taxi/ba")
public class AreaController extends BaseController {

    private final AreaService areaService;

    public AreaController(AreaService areaService) {
        this.areaService = areaService;
    }


    /**
     * 获取所有区域
     *
     * @param query 查询条件
     * @return 区域数据集合
     */
    @PostMapping("area/getAreas")
    public ApiResult getAreasByPage(@RequestBody AreaQuery query) {
        PageBean<AreaDto> pageBean = areaService.getAreasByPage(query);
        return success(pageBean);
    }


    /**
     * 根据id进行查询
     *
     * @param regionId 查询条件
     * @return 一条区域数据
     */
    @PostMapping("area/getOneArea")
    public ApiResult getOneArea(Integer regionId) {
        AreaDto area = areaService.getOneArea(regionId);
        return success(area);
    }


    /**
     * 根据区域id进行删除
     *
     * @param ids 区域信息表 主键 id 集合
     * @return
     */
    @PostMapping("area/deleteArea")
    public ApiResult deleteArea(@RequestBody Integer[] ids) {
        Integer integer = areaService.deleteArea(ids);
        return success(integer);
    }


    /**
     * 根据 form id 值 修改 区域
     *
     * @param form
     * @return
     */
    @PostMapping("area/addArea")
    public ApiResult addArea(@RequestBody AreaForm form) {
        if (form.getRegionId() == null) {
            Integer integer = areaService.addArea(form);
            return success(integer);
        }
        Integer integer = areaService.updateArea(form);
        return success(integer);
    }


}
